// pages/All/index/index.js
import S_request from '../../../utils/requestService.js';
import CONFIG from '../../../config.js';
import * as utils from '../../../utils/util.js';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0,
    allList: [],
    page: 1,
    pageSize: 20,
    hasMoreData: true,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getAllList();
  },
  getAllList: function() {
    S_request.jx.getAllList(this.data.page, (result) => {
      if (result.lists == null || result.lists.length == 0) return
      let allList = this.data.allList;
      if (result.page == 1) {
        allList = []
      } else {
        // 隐藏加载框
        wx.hideLoading();
      }
      if (result.lists.length < this.data.pageSize) {
        allList = allList.concat(result.lists);
        this.setData({
          hasMoreData: false,
        })
      } else {
        allList = allList.concat(result.lists);
        this.setData({
          hasMoreData: true,
          page: this.data.page + 1,
        })
      }
      for (let i = 0; i < allList.length; i++) {
        let item = allList[i];
        item.open_time = utils.formatTime(item.open_time, 3);
        //pay_status表示类型 0：系统随机 1：分享最多者获得
        //join表示自己是否参与 0：未参与抽奖 1：已参与
        if (item.pay_status == 0) {
          item.txt = '系统随机抽取'
        } else {
          item.txt = '邀请最多者获得'
        }
        if (item.join != 0 && item.status == 0) {
          item.btnTitle = '待开奖'
          item.btnStyle = 'background-color:#0f9134;'
        } else if (item.join == 0 && item.status == 0) {
          item.btnTitle = '点击参与'
          item.btnStyle = 'background-color:#009fd9;'
        }
      }
      this.setData({
        allList: allList
      })
    })
  },
  toDetail: function(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../../JX/detail/detail?id=' + id,
    })
  },
  //触摸变小动画
  setShrink: function(e) {
    this.setData({
      id: e.currentTarget.dataset.id,
    });
    let animation1 = wx.createAnimation({
      transformOrigin: "50% 50%",
      duration: 200,
      timingFunction: "ease"
    });
    animation1.scale(0.95).step();
    this.setData({
      animation1: animation1.export()
    });
  },
  //手指离开放大动画
  remShrink: function(e) {
    this.setData({
      id: e.currentTarget.dataset.id,
    });
    let animation1 = wx.createAnimation({
      transformOrigin: "50% 50%",
      duration: 200,
      timingFunction: "ease"
    });
    animation1.scale(1.0).step();
    this.setData({
      animation1: animation1.export()
    });
  },
  formSubmit: function(e) {
    let formId = e.detail.formId;
    console.log('form发生了submit事件，携带数据为：', e.detail.value + ", " + e.detail.formId)
    let id = e.currentTarget.dataset.id;
    console.log("all id====" + id + ",formId===" + formId);
    let param = 'formId=' + formId;
    S_request.jx.tongJiNum(id, param, (result) => {
      console.log("data====" + result);
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getAllList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.hasMoreData) {
      wx.showLoading({
        title: '玩命加载中',
        mask: true
      })
      this.getAllList();
    } else {
      if (this.data.page != 1) {
        wx.showToast({
          title: '没有更多数据',
        })
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    wx.showShareMenu({
      withShareTicket: false,
      success: function() {}
    });
    let obj = { 'pagepath': CONFIG.SHARE.path}
    S_request.jx.recordShare("-1", obj);
    return {
      title: CONFIG.SHARE.title,
      path: CONFIG.SHARE.path,
      imageUrl: CONFIG.SHARE.imageUrl,
      success: (res) => { //新版不走回调
        console.log("分享成功")
      }
    }
  }
})